package com.caochenlei.kmp;

public class ViolentMatch {
    public static void main(String[] args) {
        String str1 = "BBC ABCDAB ABCDABCDABDE";
        String str2 = "ABCDABD";
        System.out.println(violentMatch(str1, str2));
    }

    public static int violentMatch(String str1, String str2) {
        char[] ch1 = str1.toCharArray();//转换为对应字符数组
        char[] ch2 = str2.toCharArray();//转换为对应字符数组

        int i = 0;//指向ch1的下标
        int j = 0;//指向ch2的下标

        while (i < ch1.length && j < ch2.length) {
            //当前字符匹配成功
            if (ch1[i] == ch2[j]) {
                i++;
                j++;
            }
            //当前字符匹配失败
            else {
                i = i - j + 1;
                j = 0;
            }
        }

        //判断是否匹配成功
        if (j == ch2.length) {
            return i - j;
        } else {
            return -1;
        }
    }
}
